package com.zhixiang.xaec.service.system.impl;


import javax.annotation.Resource;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.zhixiang.core.dao.BaseDao;
import com.zhixiang.core.service.impl.BaseServiceImpl;
import com.zhixiang.core.util.ContextUtil;
import com.zhixiang.core.util.StringUtil;
import com.zhixiang.xaec.dao.system.AppUserDao;
import com.zhixiang.xaec.entity.system.AppUser;
import com.zhixiang.xaec.service.system.AppUserService;


@Service("appUserService")
public class AppUserServiceImpl extends BaseServiceImpl<AppUser, Long> implements AppUserService, UserDetailsService
{
//    private static Log logger = LogFactory.getLog(AppUserServiceImpl.class);

    @Autowired
    private AppUserDao appUserDao;

    public AppUser findByUserName(String userName)
    {
        return appUserDao.findByUserName(userName);
    }

    public String getCurUserInfo()
    {
        AppUser localAppUser = ContextUtil.getCurrentUser();

        StringBuffer localObject1 = new StringBuffer();
        localObject1.append("{\"success\":true,\"user\":{\"userId\":\"").append(
            localAppUser.getUserId()).append("\",\"fullname\":\"").append(
            localAppUser.getStaff().getName()).append("\",\"rights\":\"").append(
            localAppUser.getRights().toString().replace("[", "").replace("]", "")).append(
            "\",\"username\":\"").append(localAppUser.getUsername()).append("\",\"topModules\":").append(
            new Gson().toJson(localAppUser.getTopModules().values())).append("}}");

        return new Gson().toJson(localObject1.toString());
    }

    @Resource(type = AppUserDao.class)
    public void setBaseDao(BaseDao<AppUser, Long> appUserDao)
    {
        setDao(appUserDao);
    }

    @Override
    public UserDetails loadUserByUsername(String paramString)
        throws UsernameNotFoundException, DataAccessException
    {
        AppUser localAppUser = appUserDao.findByUserName(paramString);
        Hibernate.initialize(localAppUser.getAppRole());
        localAppUser.initMenuRights();
        return localAppUser;
    }

	@Override
	public String saveUser(AppUser appUser) {
	    //判断新增还是删除
	    if(appUser.getUserId()==null){
	        appUser.setPassword(StringUtil.encryptSha256(appUser.getPassword()));
	    }
//    	//"定点清除"对象缓存
//    	evict(app);
//		if(result){
            save(appUser);
//		}
//		return appUser.getUsername()+ "已存在,请重新输入账号.";
//            logger.info(new OperLog("测试日志 info"));
//            logger.debug(new OperLog("测试日志 调试"));
//            logger.warn(new OperLog("测试日志 警告"));
//            logger.error(new OperLog("测试日志 错误"));
            return null;
	}
    
}
